package com.zys.el.time.controller;

import com.zys.el.time.common.Result;
import com.zys.el.time.dto.CommentDTO;
import com.zys.el.time.dto.Page;
import com.zys.el.time.mq.constants.ModeConstants;
import com.zys.el.time.mq.constants.ModeType;
import com.zys.el.time.service.CommentService;
import com.zys.el.time.vojo.CommentVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "评论接口")
@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/comment")
public class CommentController {
    @Resource
    private CommentService commentService;

    @PostMapping("/add")
    @ApiOperation("添加评论")
    public Result addComment(@RequestBody CommentDTO commentDTO) {
        log.debug(String.valueOf(commentDTO));
        commentService.addComment(commentDTO);
        return Result.ok("添加评论成功");
    }

    @DeleteMapping("/del/{commentId}")
    @ApiOperation("删除评论By评论id")
    @ApiImplicitParam(name = "commentId", value = "评论id", required = true)
    public Result deleteComment(@PathVariable String commentId, @RequestParam("rid") String rid) {
        log.debug(commentId);
        commentService.removeByIdWithReplied(commentId, rid);
        return Result.ok("删除成功");
    }

    @GetMapping("/page")
    @ApiOperation("获取评论By帖子id")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "帖子id或根评论id", required = true),
            @ApiImplicitParam(name = "max", value = "上一页的最小值", required = true),
            @ApiImplicitParam(name = "offset", value = "上一页最小值的数量", required = true),
            @ApiImplicitParam(name = "size", value = "页大小", required = true),
            @ApiImplicitParam(name = "type", value = "类型(1：一级评论，2：二级评论)", required = true)
    })
    public Result getPageByThinkingId(@RequestParam("id") String id,
                                      @RequestParam("max") String max,
                                      @RequestParam("offset") Integer offset,
                                      @RequestParam("size") Integer size,
                                      @RequestParam("type") Integer type) {
        if (StringUtils.isBlank(id) || max == null || StringUtils.isBlank(max) || size == null) return Result.fail("缺少参数");
        log.debug(id);
        Long tid = Long.valueOf(id);
        if (type.equals(ModeConstants.COMMENT)) {
            return Result.ok(commentService.getRootCommentPage(tid, Long.valueOf(max), offset, size));
        } else if (type.equals(ModeConstants.REPLIED)) {
            return Result.ok(commentService.getRepliedPage(tid, Long.valueOf(max), offset, size));
        } else {
            return Result.fail("参数有误");
        }
    }


}
